package com.whale.cloud.util.rest.schema;

import com.alibaba.fastjson.JSONObject;
import com.whale.cloud.util.rest.condition.ETableAuthority;
import com.whale.cloud.util.rest.query.GetSysConfigure;
import com.whale.cloud.util.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/** Created by paco on 1/6/2017. */
@Component
public class FunctionManager {

  @Autowired private GetSysConfigure gsc;

  private FunctionManager() {}

  public Function getById(long tableId) {
    T_SYS_FUNCTION table = null;
    JSONObject tablejo = gsc.getTableById(tableId);
    table = JSONObject.parseObject(tablejo.toJSONString(), T_SYS_FUNCTION.class);
    Function t = SpringUtils.getBean(FunctionImpl.class); //new TableImpl(table);
    t.setFunction(table);

    return t;
  }

  public Function getByName(String tableName) {
    T_SYS_FUNCTION table = null;
    JSONObject tablejo = gsc.getTableByDBName(tableName);
    table = JSONObject.parseObject(tablejo.toJSONString(), T_SYS_FUNCTION.class);
    Function t = SpringUtils.getBean(FunctionImpl.class); //new TableImpl(table);
    t.setFunction(table);

    return t;
  }

  public boolean checkAuthority(long tableId, long userId, ETableAuthority eta) {
    return gsc.checkAuthorityTable(tableId, userId, eta);
  }
}
